<?php
namespace PsiFramework\Classes\Util\Manager;

import("Classes.Util.Plugin");
import("Classes.Util.HTTP.Request");
import("Classes.Util.HTTP.Session");
import("Classes.Util.Manager.ParametersManager");
import("Classes.Util.Parameters.DAO.PluginsDAO");

use \PsiFramework\Classes\Util\Plugin;
use \PsiFramework\Classes\Util\HTTP\Request;
use \PsiFramework\Classes\Util\HTTP\Session;
use \PsiFramework\Classes\Util\Manager\ParametersManager;
use \PsiFramework\Classes\Util\Parameters\DAO\PluginsDAO;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The PluginManager class is used to manage the plugins
 *
 * @package Util.UI
 */
class PluginManager
{

    protected static       $_singleton = null;

    protected /*Session*/  $_session ;
    protected /*Request*/  $_request ;
    protected /*Plugin[]*/ $_plugins ;


    /**
     * Constructor
     *
     * @return    ApplicationManager    instance
     */
    protected function __construct()
    {
        $this->_session = Session::getInstance();
        $this->_request = Request::getInstance();

        $plugins = $this->_session->get("plugins");
        if (is_null($plugins)) {
            $plugins = PluginsDAO::getInstance()->getPlugins();
            $this->_session->set("plugins", $plugins);
        }
        $this->_plugins =  $plugins ;
    }

    public function getPlugin(/*string*/ $pluginName=null)
    {
        if (isNullOrEmptyString($pluginName)) {
            throw new \Exception(
                "Can't retrieve plugin with a null name !"
            );
        } else if (isset($this->_plugins[$pluginName])) {
            return $this->_plugins[$pluginName] ;
        } else {
            throw new \Exception(
                "Can't retrieve plugin with name '".$pluginName."' !"
            );
        }
    }

    public function getPlugins()
    {
        return $this->_plugins ;
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return     PluginManager instance
     */
    public static function getInstance()
    {

        if (self::$_singleton==null) {
            self::$_singleton = new PluginManager();
        }

        return self::$_singleton;
    }
}
